<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests;

use View;
use App\Model\UserGroup;
use App\Model\Permission;

class UserGroupController extends AdminBaseController
{
    public function __construct()
    {
        $option = ['module_name'=>'usergroup','status'=>'usergroup_status'];
        $this->setModelObj(new UserGroup, $option);
    }

    /**
     * 获取正确的排序组合
     */
    protected function getTrueOrder($data)
    {
        $orderby = [];
        if(!array_key_exists('sort',$data)){
            $orderby['field'] = $this->model_keyname;
            $orderby['type'] = 'desc';
        }else{
            switch ($data['sort']){
                case 'usergroup_status_format':
                    $orderby['field'] = 'usergroup_status';break;
                default:
                    $orderby['field'] = $data['sort'];break;
            }
            $orderby['type'] =  $data['order'];
        }
        return $orderby;
    }

	/**
	 * 后台管理员组添加页面
	 */
	public function addDataView()
	{
        $permission_group = Permission::where('permission_status',1)->distinct()->pluck('controller_name');
        foreach ($permission_group as $key => $val){
            $permission[$key]['groupname'] = $this->exchangeName($val);
            $permission[$key]['groupsons'] = Permission::where(['permission_status'=>1,'controller_name'=>$val])->get();
        }
		return View::make('admin.usergroup.usergroupadd',['permission'=>$permission]);
	}

	private function exchangeName($val){
        switch ($val){
            case 'BannerController':
                $info = '图片管理';break;
            case 'AreaController':
                $info = '地区管理';break;
            case 'NewsController':
                $info = '新闻管理';break;
            case 'FeedbackController':
                $info = '反馈管理';break;
            case 'MemberController':
                $info = '会员管理';break;
            case 'UserController':
                $info = '管理员管理';break;
            case 'UserGroupController':
                $info = '管理员组管理';break;
            case 'SystemSetController':
                $info = '系统管理';break;
            default:
                $info = $val;
        }
        return $info;
    }

	/**
	 * 后台管理员组添加保存
	 */
	public function toAddData(Requests\UserGroupAddRequest $request)
	{
		$data = $request->except(['_token','_method']);
		if($request->get('usergroup_permission')){
			$data['usergroup_permission'] = implode('::::::',$data['usergroup_permission']);
		}
		$data['usergroup_status'] = 1;
		$data['usergroup_add_time'] = time();
		$data['usergroup_add_ip'] = $request->getClientIp();
		$re = UserGroup::insert($data);
        $jsonData = $re ? $this->getJsonData(2,'增加成功') : $this->getJsonData(0,'增加失败');
		return response()->json($jsonData);
	}
	
	/**
	 * 后台管理员组编辑页面
	 */
	public function updateDataView($usergroupid)
	{
        $permission_group = Permission::where('permission_status',1)->distinct()->pluck('controller_name');
        foreach ($permission_group as $key => $val){
            $permission[$key]['groupname'] = $this->exchangeName($val);
            $permission[$key]['groupsons'] = Permission::where(['permission_status'=>1,'controller_name'=>$val])->get();
        }
		$usergroup_info = UserGroup::find($usergroupid);
		return View::make('admin.usergroup.usergroupedit', ['permission'=>$permission, 'usergroupinfo'=>$usergroup_info]);
	}
	
	/**
	 * 后台管理员组编辑保存
	 */
	public function toUpdateData(Requests\UserGroupEditRequest $request, $usergroupid)
	{
		$data = $request->except(['_token','_method']);
		if($request->get('usergroup_permission')){
			$data['usergroup_permission'] = implode('::::::',$data['usergroup_permission']);
		}
		$re = UserGroup::where('usergroup_id',$usergroupid)->update($data);
        $jsonData = $re ? $this->getJsonData(2,'编辑成功') : $this->getJsonData(0,'编辑失败');
		return response()->json($jsonData);
	}

}